package main

import (
	"fmt"

	"gitee.com/zhangycsz/glin"
)

func main() {
	g := glin.New()
	rt := &glin.RendererTemplate{}
	rt.SetViewPath("../views/")
	rt.LoadTemplate()

	rt.SetMenuCallback(func(c glin.Context, name string) string {
		id := c.QueryParam("id") //获取的参数
		fmt.Println("id=", id)
		ab := c.Get("ab")
		fmt.Println("ab=", ab)
		fmt.Println("menu----", name)
		type Menu struct {
			Id   int
			Name string
		}
		menulist := make([]Menu, 0)
		menulist = append(menulist, Menu{
			Id:   0,
			Name: "menu0 有空格     有空格 ",
		})
		menulist = append(menulist, Menu{
			Id:   1,
			Name: "menu1",
		})
		menulist = append(menulist, Menu{
			Id:   2,
			Name: "menu3",
		})
		param := make(map[string]any)
		param["menulist"] = menulist
		content, _ := c.Out(name, param)
		content = glin.MinifyHTML(content)
		return content
	})
	//多语言翻译
	rt.SetLangCallback(func(c glin.Context, content string) string {
		return content + "lang"
	})
	//判断当前路径是否有权限
	rt.SetAuthCallback(func(c glin.Context, name string) bool {
		if name == "/auth/index1" {
			return false
		}
		return true
	})
	//设置默认
	rt.SetGlobalContext(func(c glin.Context) map[string]any {
		param := make(map[string]any)
		param["lang"] = "zh"
		return param
	})
	g.Renderer = rt

	//单独一个html显示
	g.GET("/html", func(c glin.Context) error {
		c.Render(200, "/index/test", nil)
		return nil
	})
	g.GET("/layout", func(c glin.Context) error {
		c.Set("ab", "ab----")
		err := c.RenderHtml("/index/index", nil)
		fmt.Println("err", err)
		return err
	})
	//layout布局
	g.GET("/layout/base/:id/:name", func(c glin.Context) error {
		c.Set("ab", "ab----")
		err := c.RenderHtml("/index/index", nil)
		fmt.Println("err", err)
		return err
	})
	//layout布局
	g.GET("/layout/index/:id/:name", func(c glin.Context) error {
		c.Set("ab", "ab----")
		err := c.RenderHtml("/index/index", nil)
		fmt.Println("err", err)
		return err
	})
	//layout布局
	g.GET("/layout/name/:id/:name", func(c glin.Context) error {
		c.Set("ab", "ab----")
		err := c.RenderHtml("/index/index", nil)
		fmt.Println("err", err)
		return err
	})
	//layout布局
	g.GET("/layout/index/:id/:name", func(c glin.Context) error {
		c.Set("ab", "ab----")
		err := c.RenderHtml("/index/index", nil)
		fmt.Println("err", err)
		return err
	})
	group := g.Group("/admin")
	group.Use(func(next glin.HandlerFunc) glin.HandlerFunc {
		fmt.Println("admin 中间件")

		return func(c glin.Context) error {
			return next(c)
		}
	})
	group.GET("/index", func(c glin.Context) error {
		fmt.Println("/admin/index")
		return nil
	})
	g.GET("/data", func(c glin.Context) error {
		type Menu struct {
			Id   int
			Name string
		}
		menulist := make([]Menu, 0)
		menulist = append(menulist, Menu{
			Id:   0,
			Name: "menu0        菜单名称有空格",
		})
		menulist = append(menulist, Menu{
			Id:   1,
			Name: "menu1",
		})
		menulist = append(menulist, Menu{
			Id:   2,
			Name: "menu3",
		})
		param := make(map[string]any)
		param["menulist"] = menulist

		return c.OutJson("/index/data", param)
	})

	g.Start("0.0.0.0:8085")
}
